<template>
  <div class="qyai-page">
    <div id="page-header">
      <img :src="require('@/assets/robot.gif')" @click="resetAll" class="header-logo" />
      <h1>购物 AI 问一问</h1>
    </div>
    <div class="chat-wrapper" v-if="now_tab == 1">
      <div class="chat-body" id="article-wrapper">
        <div class="chat-questions" v-if="chat_list.length > 0">
          <div class="ask-item" v-for="(item, index) in chat_list" :key="index">
            <div class="ask-question">
              <div class="ask-photo">
                <img :src="require('@/assets/user.svg')" alt="" />
              </div>
              <div class="ask-text">{{ item.prompt }}</div>
            </div>
            <div class="ask-answer" @click="scrollToBottom">
              <div class="ask-photo">
                <div class="ask-photo-ai">
                  <img :src="require('@/assets/openai.svg')" alt="" />
                </div>
              </div>
              <div class="ask-text" :id="'text-' + index" v-html="item.html"></div>
            </div>
            <div class="ask-operations" v-if="item.done">
              <div class="operations_item copy" :data-clipboard-target="'#text-' + index" title="复制文本">
                <img :src="require('@/assets/copy.svg')" />
                <span>复制</span>
              </div>
              <div class="operations_item" title="删除" @click="delAns(index)">
                <img :src="require('@/assets/trash.svg')" />
                <span>删除</span>
              </div>
            </div>
          </div>
        </div>

        <div class="chat-tutorial" v-else>
          <div>
            <h1>您可以这样提问</h1>
            <h2 style="cursor: pointer; margin-left: 50px;" @click="renderChatDemos">
              换一换<img src="@/assets/refresh.svg" style="width: 25px; vertical-align: middle;">
            </h2>
          </div>
          <div class="chat-examples">
            <div class="chat-example" v-for="(item, index) in ask_demos" :key="index" @click="doAsk(item.content)">
              <div class="chat-exp-text">{{ generateRandomEmoji() + item.content }}</div>
            </div>
          </div>
        </div>
      </div>
      <div class="chat-footer">
        <div class="chat-footer-bar">
          <div class="chat-left">
            <textarea class="chat-input" v-model="prompt" :placeholder="prompt_holder" rows="1"></textarea>
          </div>
          <div class="chat-right">
            <div class="chat-submit" @click="ask">
              <img :src="require('@/assets/send.svg')" width="100%" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import ClipboardJS from "clipboard";
import { chat, getChatDemos } from '../service/chat'

export default {
  data() {
    return {
      ask_demos: [],
      prompt: "",
      prompt_holder: "请输入您的问题",
      result: [],
      loading_txt: "AI正在思考中",
      now_tab: 1,
      chat_list: [],
      emojis: ['🎉', '💘', '🌟', '⭐', '📕', '🚀', '🎈', '❄️', '✍️', '😘', '🙊']
    };
  },
  mounted() {
    new ClipboardJS(".copy");
    this.renderChatDemos()
  },
  methods: {
    resetAll() {
      this.prompt = "";
      this.chat_list = [];
      this.renderChatDemos()
    },
    scrollToBottom() {
      window.scrollTo(0, document.documentElement.scrollHeight);
    },
    delAns(index) {
      this.chat_list.splice(index, 1);
    },
    async ask() {
      const that = this;
      if (that.prompt.length < 1) {
        this.$weui.alert("请填写要提问的内容");
        return;
      }

      that.chat_list.push({
        prompt: that.prompt,
        message: "",
        html: "<p>AI思考中...</p>",
        done: false,
      });

      const res = await chat({
        text: this.prompt
      })
      this.prompt = ''

      that.chat_list[that.chat_list.length - 1].html = res.data.output.choices[0].message.content;
      that.chat_list[that.chat_list.length - 1].done = true;

      document.getElementById("article-wrapper").scrollTop = 100000;
    },
    doAsk(qa) {
      this.prompt = qa;
      this.ask();
    },
    renderChatDemos() {
      getChatDemos().then(res => {
        this.ask_demos = res.data
      })
    },
    generateRandomEmoji() {
      const length = this.emojis.length;
      const randomNum =  Math.floor(Math.random() * (length - 1 + 1)) + 1
      return this.emojis[randomNum];
    }
  },
};
</script>
<style lang="less" scoped>
.qyai-page {
  width: 100%;
  height: 100vh;
  flex-direction: column;
  flex: 1;
  align-items: center;
  display: flex;
}
#page-header {
  height: 60px;
  line-height: 60px;
  box-sizing: border-box;
  width: 100%;
  border-bottom: 1px solid #e8e8e8;
  display: flex;
  justify-content: center;
}
#page-header .header-logo {
  width: 50px;
  height: 50px;
  object-fit: contain;
  cursor: pointer;
  position: absolute;
  left: 50px;
}
.chat-wrapper {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  flex-direction: column;
  flex: 1;
  display: flex;
  position: relative;
  overflow: hidden;
}
.chat-body {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  flex-direction: column;
  flex: 1;
  align-items: center;
  padding: 0 20px 120px;
  display: flex;
  overflow: auto;
}
.chat-body .chat-tutorial {
  max-width: 800px;
  height: 60px;
  line-height: 60px;
  flex-direction: column;
  flex: 1;
  justify-content: center;
  align-items: center;
  padding-top: 30px;
  display: flex;
}
.chat-body .chat-tutorial header {
  width: 100%;
  height: 60px;
  justify-content: center;
  display: flex;
  position: relative;
}
.chat-body .chat-tutorial header > div {
  width: 36px;
  height: 36px;
  color: #848484;
  cursor: pointer;
  border-radius: 5px;
  justify-content: center;
  align-items: center;
  display: flex;
  position: absolute;
  top: 12px;
  right: 0;
}
.chat-body .chat-tutorial header > div:hover {
  color: #555;
  background-color: #e6e6e6;
}
.chat-body .chat-tutorial header > div img {
  width: 25px;
}
.chat-body .chat-tutorial h1 {
  font-size: 20px;
  font-weight: 500;
}
.chat-body .chat-examples {
  width: 100%;
  flex-wrap: wrap;
  justify-content: space-between;
  display: flex;
}
.chat-body .chat-example {
  box-sizing: border-box;
  width: calc(33% - 12px);
  height: 80px;
  cursor: pointer;
  background: #fff;
  border-radius: 8px;
  justify-content: center;
  align-items: center;
  margin: 10px 0;
  padding: 0 16px;
  font-size: 15px;
  line-height: 24px;
  transition: background-color 0.2s;
  display: flex;
  position: relative;
  overflow: hidden;
}
.chat-body .chat-notice {
  padding: 40px 6px 12px;
}
.chat-body .chat-notice div {
  color: #848484;
  font-size: 15px;
  line-height: 1.6;
}
.chat-body .chat-notice a {
  color: #545454;
  border-bottom: 1px dotted #555;
  margin-left: 2px;
}
.switch-button > div.switch-active,
.switch-button > a.switch-active {
  background: #fff;
}
.chat-footer {
  width: 100%;
  box-sizing: border-box;
  z-index: 1;
  pointer-events: none;
  background: linear-gradient(rgba(246, 246, 246, 0), #f6f6f6 25%);
  flex-direction: column;
  flex-shrink: 0;
  align-items: center;
  padding: 30px 20px;
  display: flex;
  position: absolute;
  bottom: 0;
}
.chat-footer .chat-footer-bar {
  min-height: 60px;
  max-width: 800px;
  width: 100%;
  pointer-events: auto;
  background: #fff;
  border-radius: 8px;
  align-items: center;
  display: flex;
  box-shadow: 0 5px 7px rgba(0, 0, 0, 0.06);
  border: 1px solid #10a37f;
}
.chat-footer .chat-left {
  flex: 1;
  display: flex;
  position: relative;
}
.chat-footer .chat-input {
  box-sizing: border-box;
  width: 100%;
  min-height: 60px;
  resize: none;
  -webkit-appearance: none;
  background: 0 0;
  border: 0;
  flex: 1;
  margin: 0;
  padding: 16px 20px;
  font-size: 16px;
  line-height: 28px;
}

.chat-footer .chat-input::-webkit-input-placeholder {
  color: #999;
}
.chat-footer .chat-input.chat-input-ruler {
  height: auto;
  opacity: 0;
  pointer-events: none;
  position: absolute;
  top: 400px;
}
.chat-footer .chat-right {
  width: 60px;
  height: 60px;
  flex-shrink: 0;
  justify-content: center;
  align-items: center;
  display: flex;
}
.chat-footer .chat-submit {
  width: 36px;
  height: 36px;
  color: #fff;
  cursor: pointer;
  background: #10a37f;
  border-radius: 50%;
  justify-content: center;
  align-items: center;
  display: flex;
}
.chat-footer .chat-submit  img {
  width: 25px;
  height: 25px;
  display: block;
}
.chat-footer .chat-footer-copyright {
  pointer-events: auto;
  margin-top: 10px;
}
.chat-questions {
  width: 100%;
  max-width: 800px;
  box-sizing: border-box;
  padding: 0 32px;
}

.ask-item {
  box-sizing: border-box;
  background: #fff;
  border: 1px solid #ececec;
  border-radius: 8px;
  flex-direction: column;
  margin-top: 26px;
  padding: 0 20px;
  display: flex;
  overflow: hidden;
  position: relative;
}

.ask-item .ask-question,
.ask-item .ask-answer {
  padding: 20px 8px;
  display: flex;
}

.ask-item .ask-answer {
  border-top: 1px solid #ececec;
  padding-bottom: 28px;
  position: relative;
}

.ask-item .ask-photo {
  flex-shrink: 0;
  display: flex;
}

.ask-item .ask-photo img {
  width: 28px;
  height: 28px;
  border-radius: 50%;
}

.ask-item .ask-photo-ai {
  width: 28px;
  height: 28px;
  background: #10a37f;
  border-radius: 50%;
  justify-content: center;
  align-items: center;
  display: flex;
}

.ask-item .ask-photo-ai img {
  width: 20px;
  height: 20px;
}

.ask-item .ask-text {
  min-height: 28px;
  box-sizing: border-box;
  white-space: pre-wrap;
  flex: 1;
  margin-left: 16px;
  font-size: 16px;
  line-height: 28px;
  position: relative;
  overflow-x: scroll;
}
.ask-item .ask-text::-webkit-scrollbar {
  display: none;
}
.ask-operations {
  color: #555;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 10px;
}
.operations_item {
  height: 24px;
  cursor: pointer;
  justify-content: center;
  align-items: center;
  margin-right: 20px;
  display: flex;
  font-size: 12px;

}
.operations_item span {
  margin-left: 5px;
}
.operations_item img {
  width: 20px;
  height: 20px;
}
.operations_item:hover {
  color: #333;
  background: #e6e6e6;
}
.operations_item:active {
  background: #d5d5d5;
}
</style>
